package cn.juque.systemservice.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.systemservice.entity.UserInfo;
import cn.juque.systemservice.mapper.UserInfoMapper;
import cn.juque.systemservice.service.IGenInfoService;
import cn.juque.systemservice.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>用户信息</li>
 * </ul>
 * @date 2022-11-30 21:47:58
 **/
@Service("userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {

    private static final String USER_PREFIX = "U";

    @Resource
    private IGenInfoService sysGenService;

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 保存
     *
     * @return userId
     */
    @Override
    public String saveInfo() {
        long num = this.sysGenService.getGen(USER_PREFIX);
        String userId = StrUtil.fillBefore(String.valueOf(num), '0', 10);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        boolean flag = SqlHelper.retBool(this.userInfoMapper.insert(userInfo));
        if (!flag) {
            throw new AppException(MessageEnum.SYSTEM_FAIL.getCode(), "用户信息保存失败");
        }
        return USER_PREFIX + userId;
    }
}
